package com.wisezone.permission.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.wisezone.common.exception.WzServiceException;
import com.wisezone.common.exception.WzWebException;
import com.wisezone.common.service.impl.BaseServiceImpl;
import com.wisezone.common.utils.ConstantUtils;
import com.wisezone.permission.bean.entity.WzRescEntity;
import com.wisezone.permission.bean.form.WzRescForm;
import com.wisezone.permission.bean.search.WzRescSearch;
import com.wisezone.permission.dao.WzRescDao;
import com.wisezone.permission.service.WzRescService;

/**
 * 
 *资源; InnoDB free: 24576 kB
 * 
 * @author create by create date 2010-07-29 14:44:44 update date 2010-07-29
 *         14:44:44
 * 
 */
public class WzRescServiceImpl extends BaseServiceImpl implements WzRescService {

	/**
	  *
	  *
	  *
	  */
	private WzRescDao wzRescDao;

	/**
	 * 获得父资源
	 * 
	 * @return
	 * @throws WzWebException
	 */
	public List<WzRescEntity> getParentList() throws WzWebException {

		WzRescSearch searchEntity = new WzRescSearch();
		searchEntity.setFuncType(ConstantUtils.RESC_FUNC_TYPE_MENU);
		List<WzRescEntity> list = this.wzRescDao.listAll(searchEntity);
		return list;
	}

	/**
	 * 获得菜单资源链接
	 * 
	 * @return
	 * @throws WzWebException
	 */
	public List<WzRescEntity> getMenuResList() throws WzWebException {

		WzRescSearch searchEntity = new WzRescSearch();
		searchEntity.setFuncType(ConstantUtils.RESC_FUNC_TYPE_MENU);
		List<WzRescEntity> list = this.wzRescDao.listAll(searchEntity);
		return list;
	}

	/**
	 * 获得资源列表
	 * 
	 * @param name
	 *            菜单名称
	 * @return
	 * @throws WzServiceException
	 */
	public List<WzRescForm> getRescList(String name) throws WzServiceException {

		WzRescSearch searchEntity = new WzRescSearch();

		// 获得一级资源
		searchEntity.setParentId(0L);
		searchEntity.setName(name);
		searchEntity.setEnableLike(true);
		List<WzRescForm> firstList = this.wzRescDao.listFormAll(searchEntity);

		// 获得二级资源
		searchEntity = new WzRescSearch();
		searchEntity.setName(name);
		searchEntity.setEnableLike(true);
		searchEntity.setSqlClause("t1.parent_id > 0");
		List<WzRescForm> secondList = this.wzRescDao.listFormAll(searchEntity);

		// 获得三级（按钮）资源
		searchEntity = new WzRescSearch();
		searchEntity.setName(name);
		searchEntity.setEnableLike(true);
		searchEntity.setSqlClause("t1.parent_id > 0");
		searchEntity.setFuncType(ConstantUtils.RESC_FUNC_TYPE_BTN);
		List<WzRescForm> thirdList = this.wzRescDao.listFormAll(searchEntity);

		List<WzRescForm> list = new ArrayList<WzRescForm>();
		if (firstList == null || firstList.size() == 0) {
			return list;
		}
		for (Iterator<WzRescForm> it = firstList.iterator(); it.hasNext();) {
			WzRescForm entity = it.next();
			list.add(entity);
			if (secondList == null || secondList.size() == 0) {
				continue;
			}
			for (Iterator<WzRescForm> childIt = secondList.iterator(); childIt
					.hasNext();) {
				WzRescForm childEntity = childIt.next();
				if (childEntity.getParent() != null
						&& childEntity.getParent().getId().equals(
								entity.getId())) {
					entity.addChild(childEntity);

					// 添加三级资源
					if (thirdList == null || thirdList.size() == 0) {
						continue;
					}
					for (Iterator<WzRescForm> threeChildIt = thirdList
							.iterator(); threeChildIt.hasNext();) {

						WzRescForm threeChildEntity = threeChildIt.next();
						if (threeChildEntity.getParent() != null
								&& threeChildEntity.getParent().getId().equals(
										childEntity.getId())) {
							childEntity.addChild(threeChildEntity);
						}
					}
				}
			}
		}
		return list;
	}

	
	/**
	 * 获得资源id和名称的map
	 * 
	 * @param id
	 * @return
	 */
	public Map<String, String> getMapByRoleId(Long id)
			throws WzServiceException {

		if (id == null) {
			throw new IllegalArgumentException("角色ID不能为空");
		}

		List<WzRescEntity> list = this.wzRescDao.listByRoleId(id);

		if (list == null || list.size() == 0) {
			return null;
		}
		Map<String, String> map = new HashMap<String, String>();
		String ids = "";
		String names = "";

		for (Iterator<WzRescEntity> it = list.iterator(); it.hasNext();) {
			WzRescEntity entity = it.next();
			ids += "," + entity.getId();
			names += "," + entity.getName();
		}
		ids = ids.substring(1);
		names = names.substring(1);
		map.put("ids", ids);
		map.put("names", names);
		return map;

	}

	public void setWzRescDao(WzRescDao wzRescDao) {
		this.setBaseDao(wzRescDao);
		this.wzRescDao = wzRescDao;
	}

}